from dataclasses import field
from ensurepip import bootstrap
from django.shortcuts import render
from app01 import models
from app01.utils.pagination import Pagination
from django import forms
from django.core.validators import ValidationError
from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from app01.utils.bootstrape import BootStrapModelForm,BootStrapForm
from django.views.decorators.csrf import csrf_exempt
from  decimal import  Decimal

def worker(request):
    return render(request,'worker.html')


def order_info(request):
    queryset=models.Order.objects.filter(state2=1)
    querysets=models.Client.objects.all()
    page_obj=Pagination(request,queryset)
    contxt={'queryset':page_obj.page_queryset,
        'page_string':page_obj.html(),
         'querysets':querysets,
        "title":'订单信息',
       
    }
    return render(request,'order_info.html',contxt)

class WorkerArriveModelForm(BootStrapModelForm):
    class Meta:
        model=models.Order_examine
        fields=["wid","oid"]

@csrf_exempt
def arrive(request):
    if request.method=="GET":
        form=WorkerArriveModelForm()
        return render(request,'change3.html',{"form":form,"title":'确认订单'})
    form=WorkerArriveModelForm(data=request.POST)
    if form.is_valid():
        admin_object=models.Order_examine.objects.filter(**form.cleaned_data,state3=3).first()
        if not admin_object:
            form.add_error("oid","工号或订单编号错误")
            return render(request,'change3.html',{'form':form,'title':'确认订单'})
        models.Order_examine.objects.filter(oid=form.cleaned_data["oid"]).update(state1=2)
        
        return render(request,'change3.html')
    return render(request,'change3.html',{"form":form,"title":'确认订单'})



@csrf_exempt
def finish_order(request,wid):
    total=Decimal(0)
    base_salary=models.Water_workers.objects.get(id=wid).base_salary
    queryset=models.Order_examine.objects.filter(state1=2,wid=wid)
    queryset2=models.Order_examine.objects.values("num")
    if models.Order_examine.objects.filter(state1=2,wid=wid):
        queryset3=list(models.Order_examine.objects.filter(wid=wid,state1=2).values('wid', 'num'))
        commision=models.Water_workers.objects.get(id=queryset3[0]['wid']).commision
   
        for obj in queryset3:
            total = total + commision * Decimal(obj['num'])
            
    total=total+base_salary
    page_obj=Pagination(request,queryset)
    contxt={'queryset':page_obj.page_queryset,
        'page_string':page_obj.html(),
        "title":'完成订单',
        "queryset2":queryset2,
        "total":total,
    }
    return render(request,'finish_order.html',contxt)

def worker_salary(request,nid):
    total=Decimal(0)
    queryset2=models.Water_workers.objects.all
    if models.Order_examine.objects.filter(wid=nid):
        queryset=list(models.Order_examine.objects.filter(wid=nid,state1=2).values('wid', 'num'))
        commision=models.Water_workers.objects.get(id=queryset.first()['wid']).commision
        for obj in queryset:
            total = total + commision * Decimal(obj['num'])
        print(total)
    page_obj=Pagination(request,queryset2)
    contxt={'queryset':page_obj.page_queryset,'page_string':page_obj.html(),"total":total}
    return render(request,'worker_salary.html',contxt)

def worker_examine(request,nid):
    queryset=models.Order_examine.objects.filter(wid=nid).exclude(state3=1).all()
    # queryset=models.Order.objects.filter(oid=oid,state2=2).exclude(state3=1).all()
    return render(request,'worker_examine.html',{'queryset':queryset})
       
